from sqlalchemy import Column, Integer, Date, Float, String, DateTime
from sqlalchemy.sql import func
from backend.config.database import Base


class WeatherDaily(Base):
    """每日气象数据表 - 兼容Open-Meteo和VisualCrossing两个API"""
    __tablename__ = "weather_daily"
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键')
    dt = Column(Date, unique=True, nullable=False, comment='日期')
    
    # 温度相关 - 两个API共有字段
    t_max = Column(Float, nullable=True, comment='最高气温(°C) - Open-Meteo:temperature_2m_max, VisualCrossing:tempmax')
    t_min = Column(Float, nullable=True, comment='最低气温(°C) - Open-Meteo:temperature_2m_min, VisualCrossing:tempmin')
    
    # 湿度 - 两个API共有字段
    humidity = Column(Float, nullable=True, comment='相对湿度(%) - Open-Meteo:humidity, VisualCrossing:humidity')
    
    # 降水相关 - 两个API共有字段
    precip = Column(Float, nullable=True, comment='降水量(mm) - Open-Meteo:precipitation_sum, VisualCrossing:precip')
    snow = Column(Float, nullable=True, comment='降雪量(mm) - Open-Meteo:snow, VisualCrossing:snow')
    
    # 风相关 - 两个API共有字段
    wind_dir = Column(Float, nullable=True, comment='风向(°) - Open-Meteo:winddirection_10m, VisualCrossing:winddir')
    ws_max = Column(Float, nullable=True, comment='最大风速(km/h) - Open-Meteo:windspeed_10m_max, VisualCrossing:windspeed')
    
    # 气压 - 两个API共有字段
    pressure = Column(Float, nullable=True, comment='气压(hPa) - Open-Meteo:pressure_msl, VisualCrossing:pressure')
    
    # 紫外线指数 - 两个API共有字段
    uv_index = Column(Float, nullable=True, comment='紫外线指数 - Open-Meteo:uv_index_max, VisualCrossing:uvindex')
    
    # 数据源标识
    data_source = Column(String(20), nullable=True, comment='数据源(openmeteo/visualcrossing)')
    
    # 时间戳
    created_at = Column(DateTime, default=func.now(), comment='入库时间')
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment='更新时间')
    
    class Config:
        from_attributes = True 